/**
 * 房源信息公共window
 */
Ext.define('component.room.InfoWindow', {
	extend : 'Ext.window.Window',
	config : {
		roomId : '',
		jumpFlag:'0'
	},
	initComponent : function() {
		var me = this;
		var roomId = this.roomId;
		//0 跳转房源详情  1跳转销控列表
		var jumpFlag=this.jumpFlag;
		var fromPanel =   Ext.create("Ext.form.Panel", {
			collapsible : false, 
			autoScroll : false, 
			defaultType : 'textfield',
			defaults : {
				anchor : '100%'
			},
			fieldDefaults : {
				flex : 1
			},
			items : [ {
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '房产状态',
					name:'roomStatus'
				}, {
					xtype : 'displayfield',
					fieldLabel : '计价方式',
					name:'valuationType'
				}]
			}, {
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '建筑面积',
					name:'buildingArea'
				}, {
					xtype : 'displayfield',
					fieldLabel : '套内面积',
					name:'innerArea'
				} ]
			}, {
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '分摊面积',
					name:'sharingArea'
					
				}, {
					xtype : 'displayfield',
					fieldLabel : '花园面积',
					name:'gardenArea'
					
				} ]
			},{
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '地上面积',
					name:'aboveGroundArea'
					
				}, {
					xtype : 'displayfield',
					fieldLabel : '地下面积',
					name:'basementArea'
					
				}]
			},{
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '赠送面积',
					name:'freeArea'
					
				} ,{
					xtype : 'displayfield',
					fieldLabel : '建筑单价',
					name:'buildingPriceN'
					
				}]
			},{
				xtype : "container",
				layout : "hbox",
				items : [ {
					xtype : 'displayfield',
					fieldLabel : '套内单价',
					name:'innerPriceN'
					
				}, {
					xtype : 'displayfield',
					fieldLabel : '总价',
					name:'scheme_total_price'
					
				}]
			}
			]
		});
		
		var fromPanleImages = Ext.create('Ext.Panel', {
			width:270,
	       	height:200,
	        frame: true,
	        collapsible: false,
	        autoScroll:true,
	        title:'户型图',
	        id:'images'
	    });
		
		var roomInfo = Ext.create('Ext.Panel', {
	  		width:400,
	       	height:200,
	        frame: true,
	        collapsible: false,
	        autoScroll:true,
	        items:[fromPanel]
	    });
		
		var tableResoce=Ext.create('Ext.Panel', {
			region : 'north',
		    frame: true,
		    collapsible: false,
		    autoScroll:true,
		    layout:'hbox',
		    items:[roomInfo,fromPanleImages]
		});
		
		var gridStore = Ext.create('Ext.data.Store', {
	        fields: ["businessDynamics","businessHref","remark","createTime","createManageCN"],
	        pageSize: 10,  
	        remoteSort: false,
	        proxy: {
	            type: 'ajax',
	            url: jypath+'/marketing/roomChangeHistory/list?roomId='+roomId,
	            reader: {   
	                type: 'json', 
	                root: 'rows',  
	                totalProperty: 'total' 
	            }
	        },
	        autoLoad: true  //即时加载数据
	    });
		
		
		var gridPanel = Ext.create('Ext.grid.Panel', {
			region : 'center',
		  	store: gridStore,
		    columnLines: true,
		    columns: [{
		        text: '业务状态',
		        dataIndex: 'businessDynamics',
		        renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
		        	var obj = record.data["businessHref"];
		        	if(obj==""||obj==null){
		        		return value;
		        	}else{
		        		var idval = getStrValue("id",obj,"1").replace("id=","");
		        		if(value=="认购"){
		        			return "<a href='javascript:parent.openMenu(\"0\",\""+idval+"\",\"\",\"认购详情\",\"/customer/subscribe/toView?id="+idval+"&returnUrl=,marketing,pin,index\")' style='text-decoration:none'>认购</a>"
		        		}else if(value=="签约"){
		        			var tabKey = "sig_"+idval;
		        			return "<a href='javascript:parent.openMenu(\"0\",\""+tabKey+"\",\"\",\"签约详情\",\"/customer/signed/toView?id="+idval+"&returnUrl=,marketing,pin,index\")' style='text-decoration:none'>签约</a>";
		        		}else if(value=="退认购"){
		        			var tabKey = "ret_"+idval;
				    		return "<a href='javascript:parent.openMenu(\"0\",\""+tabKey+"\",\"\",\"退认购\",\"/customer/subscribe/toRetreatView?id="+idval+"&1=1\")' style='text-decoration:none'>退认购</a>";
		        		}else if(value=="换房"){
		        			 var tabKey = "busDetail_"+idval;
			     	       	 var url = "/marketing/businessAbnorma/businessDetail?id="+idval+"&1=1";
			     	       	 return "<a href='javascript:parent.openMenu(\"0\",\""+tabKey+"\",\"\",\"换房信息\",\""+url+"\")' style='text-decoration:none'>换房</a>";
		        		}else if(value=="签约换房"){
		        			var tabKey = "tfcInfo_"+idval;
			     	       	var url = "/marketing/changeroom/changeDetail?id="+businessAbnormaId+"&1=1";
			     	        return "<a href='javascript:parent.openMenu(\"0\",\""+tabKey+"\",\"\",\"签约换房信息\",\""+url+"\")' style='text-decoration:none'>签约换房</a>";
		        		}
		        	}
		        	var showHtml = "<a href='"+jypath+obj+"&menu="+menu+"' style=''>"+value+"</a>";
		        	return showHtml;
		        }
		    },{
		        text     : '备注', 
		        dataIndex: 'remark'
		    },{
		        text     : '操作时间',
		        dataIndex: 'createTime',
		        renderer:function(value){
		        	return Ext.Date.format(new Date(value),'Y-m-d H:i:s');
		        }
		    },{
		        text     : '操作人',
		        dataIndex: 'createManageCN'
		    }],
		    selModel: {
		        selType: 'rowmodel'
		    },
		    title: '房源动态',
		    viewConfig: {
		        stripeRows: true
		    },
		    dockedItems: [{  
		        xtype: 'pagingtoolbar',  
		        store: gridStore,   // GridPanel中使用的数据  
		        dock: 'bottom',  
		        displayInfo: true,  
		        emptyMsg:"没有数据",  
		        displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据"
		    }],
		    tbar:[
		    	{id: "btn_retain", hidden:maketingRetainAdd,text:'保留',icon:jypath+'/resources/ExtJSicons/flag/flag_red.png',
		    		handler:function(){
		        		retain(roomId,2,null);
		         	}
		  		},"-",
		     	/*{id: "btn_paydeposit", hidden:true,text:'认筹',icon:jypath+'/resources/ExtJSicons/flag/flag_yellow.png',
		     		handler:function(){
		           		retain(roomId,3,1); 
		      		}
				},"-",*/
		     	{id: "btn_subscribe", hidden:marketingSubscribeAdd,text:'认购',icon:jypath+'/resources/ExtJSicons/flag/flag_purple.png',
		     		handler:function(){
		         		retain(roomId,4,1); 
		     		}
		        },"-",
		     	{id: "btn_signed", hidden:marketingSignedAdd,text:'签约',icon:jypath+'/resources/ExtJSicons/flag/flag_blue.png',
		     		handler:function(){
		       			retain(roomId,5,1); 
		 			}
				},"-",
		     	{id: "btn_sellControl",hidden:marketingSellControlAdd,text:'销控',icon:jypath+'/resources/ExtJSicons/flag/flag_pink.png',
		     		handler:function(){
		         		retain(roomId,6,null); 
		          	}
		     	}
		     ]
		});

		function retain(roomId,type,valid){
			Ext.Ajax.request({
				url:jypath+'/marketing/room/findRoomById',
	    		params:{
	    			"id":roomId
	    		},
	    		callback: function (options, success, response) {
	    			 var obj = JSON.parse(response.responseText);
	    			 locationPage(type,roomId,obj.projectId,obj.roomStatus);
	    		}
			});
		}
		
		function getStrValue(fieldName, str, flag) {
			  var fieldIndex = str.indexOf(fieldName + "="); //第一次出现指定字符串的位置
			  var fieldRemain = str.substr(fieldIndex, str.length);
			  if (flag == "1"){
				  var ind = fieldRemain.indexOf("&");
				  if(ind<0){
					  return fieldRemain.substr(0, fieldRemain.length);
				  }else{
					  return fieldRemain.substr(0, ind);
				  }
			  }else {
			   return fieldRemain.substr(0, str.length);
			  }
		}
		
		function locationPage(type,roomId,projectId,roomStatus){
			 if(type=="2"){
				 //保留
				 var tabKey = "retein_"+roomId;
				 var url = '/marketing/retain/addRetainPage?roomId='+roomId+'&projectId='+projectId+'&jumpFlag='+jumpFlag;
				 parent.openMenu("0",tabKey,"","保留",url);
			 }else if(type=="3"){
				 //认筹
				 location.href=jypath + "/customer/payDeposit/toAdd?roomId="+roomId+"&menu="+menu+"&returnUrl=,marketing,room,index(menu)"+menu+"&projectId="+projectId+"&jumpFlag="+jumpFlag;
			 }else if(type=="6"){
				 //销控
				 var tabKey = "sell_"+roomId;
				 var url = '/marketing/sellControl/addSellControlView?roomId='+roomId+'&projectId='+projectId+'&jumpFlag='+jumpFlag;
				 parent.openMenu("0",tabKey,"","销控",url);
			 }else if(type=="4"){
				//认购
				 if(roomStatus=="4"){
					 Ext.Msg.alert('提示', '无法操作，房源已被认购！');
					 return;
				 }
				 if(roomStatus=="5"){
					 Ext.Msg.alert('提示', '无法操作，房源已被签约！');
					 return;
				 }
				 var tabKey = "subKey_"+roomId;
				 var url = '/customer/subscribe/toAdd?roomId='+roomId+'&projectId='+projectId+'&jumpFlag='+jumpFlag;
				 parent.openMenu("0",tabKey,"","认购",url);
			 }else if(type=="5"){
				 //签约
				 if(roomStatus=="4"){
					 Ext.Msg.alert('提示', '房源已被认购。如需转签约，请前往“认购记录详情”页面进行操作！');
					 return;
				 }
				 if(roomStatus=="5"){
					 Ext.Msg.alert('提示', '无法操作，房源已被签约！');
					 return;
				 }
				 var tabKey = "addSigned";
				 var url = '/customer/signed/toAdd?roomId='+roomId+'&projectId='+projectId+'&jumpFlag='+jumpFlag;
				 parent.openMenu("0",tabKey,"","签约",url);
			 }/*else if(type=="4"||type=="5"){
				    //认购、签约
				 	if(roomStatus!="1"){
				 		var bus = 2;
					 	if(type=="4"){
					 		bus=1;
					 	}
                 
					 	if(roomStatus=="3" || roomStatus=="4"){
				 			//房源状态为认筹  点击认购或者签约
					 		Ext.Ajax.request({
								url:jypath+'/marketing/room/findBusCustomerInfo',
					    		params:{
					    			"roomId":roomId,
					    			"businessCode":bus
					    		},
					    		callback: function (options, success, response) {
					    			if(response.responseText==null||response.responseText==""){
					    				pageLocation(type, roomId, menu, projectId);
					    			}else{
					    				openShow(roomId, bus, menu, projectId);
					    			}
					    		}
							});
						}else{
				 		    pageLocation(type, roomId, menu, projectId);
				 		}
				 	}else{
				 		    pageLocation(type, roomId, menu, projectId);
				 	}
			 }*/
		}
			
		function pageLocation(type,roomId,menu,projectId){
			if(type=="4"){
	 			 location.href=jypath + "/customer/subscribe/toAdd?roomId="+roomId+"&menu="+menu+"&returnUrl=,marketing,room,index(menu)"+menu+"&projectId="+projectId+"&jumpFlag="+jumpFlag;
	 		}else{
	   			 location.href=jypath+ "/customer/signed/toAdd?roomId="+roomId+"&menu="+menu+"&returnUrl=,marketing,room,index(menu)"+menu+"&projectId="+projectId+"&jumpFlag="+jumpFlag;
	 		}
		}
		
		function openShow(roomId,bus,menu,projectId){
			 var selectWindow=Ext.create('component.business.window',{
		 	        roomId:roomId,
		 	        businessStatus:bus,
		 	        menu:menu,
		 	        projectId:projectId
		 	    });
			    selectWindow.show();
		}
		
		fromPanel.getForm().load({
			url:jypath+'/marketing/room/findRoomInfo',
			params:{'roomId':roomId},
			success:function(form, action){
				var imgs="<img style='width:250px;height:160px;' src='"+action.result.data.houseTypeImg+"'/>";
				Ext.getCmp("images").body.update(imgs);
				var roomStatusDesc = action.result.data.roomStatus;
				Ext.getCmp("btn_retain").hide();
				Ext.getCmp("btn_paydeposit").hide();
				Ext.getCmp("btn_subscribe").hide();
				Ext.getCmp("btn_signed").hide();
				Ext.getCmp("btn_sellControl").hide();
				if("可售" == roomStatusDesc){
					//可售,应显示全部按钮
					//判断是否具备功能权限进行展示
					if(!maketingRetainAdd){
						Ext.getCmp("btn_retain").show();
					}
					if(!marketingPayDepositAdd){
						Ext.getCmp("btn_paydeposit").show();
					}
					if(!marketingSubscribeAdd){
						Ext.getCmp("btn_subscribe").show();
					}
					if(!marketingSignedAdd){
						Ext.getCmp("btn_signed").show();
					}
					if(!marketingSellControlAdd){
						Ext.getCmp("btn_sellControl").show();
					}
				}else if("认筹" == roomStatusDesc || "小订" == roomStatusDesc){
					//认筹,只展示认筹、认购、签约按钮.
					//显示前,判断是否拥有功能权限
					console.log(marketingPayDepositAdd);
					if(!marketingPayDepositAdd){
						Ext.getCmp("btn_paydeposit").show();
					}
					if(!marketingSubscribeAdd){
						Ext.getCmp("btn_subscribe").show();
					}
					if(!marketingSignedAdd){
						Ext.getCmp("btn_signed").show();
					}
				}else if("认购" == roomStatusDesc){
					//认购,只展示签约按钮.
					//显示前,判断是否拥有功能权限
					if(!marketingSignedAdd){
						Ext.getCmp("btn_signed").show();
					}
				}else if("签约" == roomStatusDesc){
					//不展示任何按钮
				}else if("销控" == roomStatusDesc){
					//不展示任何按钮
				}else if("暂不可售" == roomStatusDesc){
					//不展示任何按钮
				}
			},   
			failure: function(form, action) {
		        Ext.Msg.alert("加载失败", action.result.errorMessage);
		    }
		});
			
			
	
		var defaultConfig = {
			frame:true,
			title : '房源详情',
			width: 700,
		    height: 500,
	        modal: true,
			layout : 'border',
			items:[tableResoce,gridPanel]
		};
		Ext.apply(this,defaultConfig);
		this.callParent();
	}
});